//
//  NSString+HQCategory.m
//  PerfectLifeTwo
//
//  Created by 黄华强 on 2018/2/23.
//  Copyright © 2018年 PerfectLife. All rights reserved.
//

#import "NSString+HQCategory.h"

@implementation NSString (HQCategory)


+ (CGSize)getStringSize:(NSString *)string maxSize:(CGSize)maxSize font:(UIFont *)font {
    
    NSDictionary *attributes = @{NSFontAttributeName:font};
    
    CGRect rect = [string boundingRectWithSize:maxSize
                                       options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                    attributes:attributes
                                       context:nil];
    
    return CGSizeMake(rect.size.width + 1, rect.size.height + 1);
}


+ (NSDecimalNumber *)reviseStringToNumber:(NSString *)string {
    
    double conversionValue        = (double)[string floatValue];
    NSString *doubleString        = [NSString stringWithFormat:@"%.1f", conversionValue];
    NSDecimalNumber *revise    = [NSDecimalNumber decimalNumberWithString:doubleString];
    
    
    return revise;
}

+ (NSString *)reviseStringToString:(NSString *)string{
    
    double conversionValue        = (double)[string floatValue];
    NSString *doubleString        = [NSString stringWithFormat:@"%f", conversionValue];
    NSDecimalNumber *revise    = [NSDecimalNumber decimalNumberWithString:doubleString];
    
    return [revise stringValue];
}

+ (CGFloat)reviseStringToFloat:(NSString *)string{
    
    double conversionValue        = (double)[string floatValue];
    NSString *doubleString        = [NSString stringWithFormat:@"%f", conversionValue];
    NSDecimalNumber *revise    = [NSDecimalNumber decimalNumberWithString:doubleString];
    
    return [revise floatValue];
}

+ (NSString *)timeStampToDateString:(NSString *)timeStamp formatterStr:(NSString *)formatterStr {
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[timeStamp doubleValue]];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:formatterStr];
    NSString *dateString = [formatter stringFromDate: date];
    
    return dateString;
    
}

+ (NSString *)dateToDateString:(NSDate *)date formatterStr:(NSString *)formatterStr {
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:formatterStr];
    NSString *dateString = [formatter stringFromDate: date];
    
    return dateString;
    
}



//根据身份证号获取生日
+ (NSString *)birthdayStrFromIdentityCard:(NSString *)numberStr
{
    NSMutableString *result = [NSMutableString stringWithCapacity:0];
    NSString *year = nil;
    NSString *month = nil;
    
    BOOL isAllNumber = YES;
    NSString *day = nil;
    if([numberStr length]<14)
        return result;
    
    //**截取前14位
    NSString *fontNumer = [numberStr substringWithRange:NSMakeRange(0, 13)];
    
    //**检测前14位否全都是数字;
    const char *str = [fontNumer UTF8String];
    const char *p = str;
    while (*p!='\0') {
        if(!(*p>='0'&&*p<='9'))
            isAllNumber = NO;
        p++;
    }
    if(!isAllNumber)
        return result;
    
    year = [numberStr substringWithRange:NSMakeRange(6, 4)];
    month = [numberStr substringWithRange:NSMakeRange(10, 2)];
    day = [numberStr substringWithRange:NSMakeRange(12,2)];
    
    [result appendString:year];
    [result appendString:@"-"];
    [result appendString:month];
    [result appendString:@"-"];
    [result appendString:day];
    return result;
}

//根据省份证号获取年龄
+ (NSString *)getIdentityCardAge:(NSString *)numberStr
{
    
    NSDateFormatter *formatterTow = [[NSDateFormatter alloc]init];
    [formatterTow setDateFormat:@"yyyy-MM-dd"];
    NSDate *bsyDate = [formatterTow dateFromString:[self birthdayStrFromIdentityCard:numberStr]];
    
    NSTimeInterval dateDiff = [bsyDate timeIntervalSinceNow];
    
    int age = trunc(dateDiff/(60*60*24))/365;
    
    return [NSString stringWithFormat:@"%d",-age];
}



@end
